package com.fan.shop.views.bean;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

import org.apache.log4j.Logger;

import com.fan.shop.businesslogic.TransactionReferenceUpdateProcess;
import com.fan.shop.domain.TransactionReference;
import com.fan.shop.services.TransactionReferenceService;


@ManagedBean(name = "transactionReferencesBean")
@SessionScoped
public class TransactionReferencesBean  extends ShopBean{
	
	private static Logger logger = Logger.getLogger(TransactionReferencesBean.class);

	@ManagedProperty(value = "#{transactionReferenceService}")
	private TransactionReferenceService transactionReferenceService;
	
	private List<TransactionReference> transactionReferences;
	
	
	private String editTransactionReferenceName;
	private String editTransactionReferencePrefix;
	private String editTransactionReferenceDescription;
	private int editTransactionReferenceStart;
	private int editTransactionReferenceLast;
	private int editTransactionReferenceMax;
	

	
	

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		TransactionReferencesBean.logger = logger;
	}

	public String getEditTransactionReferenceName() {
		return editTransactionReferenceName;
	}

	public void setEditTransactionReferenceName(String editTransactionReferenceName) {
		this.editTransactionReferenceName = editTransactionReferenceName;
	}

	public String getEditTransactionReferencePrefix() {
		return editTransactionReferencePrefix;
	}

	public void setEditTransactionReferencePrefix(
			String editTransactionReferencePrefix) {
		this.editTransactionReferencePrefix = editTransactionReferencePrefix;
	}

	public String getEditTransactionReferenceDescription() {
		return editTransactionReferenceDescription;
	}

	public void setEditTransactionReferenceDescription(
			String editTransactionReferenceDescription) {
		this.editTransactionReferenceDescription = editTransactionReferenceDescription;
	}

	public int getEditTransactionReferenceStart() {
		return editTransactionReferenceStart;
	}

	public void setEditTransactionReferenceStart(int editTransactionReferenceStart) {
		this.editTransactionReferenceStart = editTransactionReferenceStart;
	}

	public int getEditTransactionReferenceLast() {
		return editTransactionReferenceLast;
	}

	public void setEditTransactionReferenceLast(int editTransactionReferenceLast) {
		this.editTransactionReferenceLast = editTransactionReferenceLast;
	}

	public int getEditTransactionReferenceMax() {
		return editTransactionReferenceMax;
	}

	public void setEditTransactionReferenceMax(int editTransactionReferenceMax) {
		this.editTransactionReferenceMax = editTransactionReferenceMax;
	}

	public TransactionReferenceService getTransactionReferenceService() {
		return transactionReferenceService;
	}

	public void setTransactionReferenceService(
			TransactionReferenceService transactionReferenceService) {
		this.transactionReferenceService = transactionReferenceService;
	}

	public List<TransactionReference> getTransactionReferences() {
		transactionReferences = transactionReferenceService.getAllTransactionReferences();
		return transactionReferences;
	}

	public void setTransactionReferences(
			List<TransactionReference> transactionReferences) {
		this.transactionReferences = transactionReferences;
	}
	
	
	public String updateTransactionReferenceAction()
	{
		logger.debug("editTransactionReferenceAction()");
		logger.debug("TransactionReference: " + editTransactionReferenceName + " is going to be edited");
		
		Map<String, String> map = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		editTransactionReferenceName = map.get("form:edit_name");
		
		
		
		TransactionReferenceUpdateProcess transactionReferenceUpdateProcess = new TransactionReferenceUpdateProcess();
		transactionReferenceUpdateProcess.setName(editTransactionReferenceName);
		transactionReferenceUpdateProcess.setPrefix(editTransactionReferencePrefix);
		transactionReferenceUpdateProcess.setDescription(editTransactionReferenceDescription);
		transactionReferenceUpdateProcess.setStart(editTransactionReferenceStart);
		transactionReferenceUpdateProcess.setLast(editTransactionReferenceLast);
		transactionReferenceUpdateProcess.setMax(editTransactionReferenceMax);
		transactionReferenceUpdateProcess.setTransactionReferenceService(transactionReferenceService);
		transactionReferenceUpdateProcess.process();
		
		
		return "management/transactionReferences";
	}
	
	public String deleteTransactionReferenceAction(String name)
	{
		if(name != null && name.length() > 0)
		{
			editTransactionReferenceName = name;
		}
		logger.debug("deleteTransactionReferenceAction()");
		logger.debug("TransactionReference: " + editTransactionReferenceName + " is going to be deleted");
		TransactionReference transactionReference = transactionReferenceService.getTransactionReference(editTransactionReferenceName);
		
		transactionReferenceService.deleteTransactionReference(transactionReference);
		return "management/transactionReferences";
	}
	
	public String addTransactionReferenceAction()
	{
		return "management/newTransactionReference";
	}
	
	public void loadTransactionReferenceAction(AjaxBehaviorEvent event)
	{
		Map<String, String> map = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		editTransactionReferenceName = map.get("form:edit_name");
		
		if(editTransactionReferenceName != null && editTransactionReferenceName.length()>0)
		{
			if(transactionReferenceService.hasTransactionReference(editTransactionReferenceName))
			{
				TransactionReference transactionReference = transactionReferenceService.getTransactionReference(editTransactionReferenceName);
				editTransactionReferenceDescription = transactionReference.getDescription();
				editTransactionReferencePrefix = transactionReference.getPrefix();
				editTransactionReferenceStart = transactionReference.getStart();
				editTransactionReferenceLast = transactionReference.getLast();
				editTransactionReferenceMax = transactionReference.getMax();
			}
		}
	}
	
	
	
	
}
